home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / MacHaskell 2.2 / ast / valdef-structs.scm < prev   
Encoding:
Text File  |  1994-09-27  |  7.9 KB  |  286 lines  |  [TEXT/CCL2]

  1. ;;; File: ast/valdef-structs    Author: John
  2.  
  3. ;;; Ast structure for local declarations
  4.  
  5. ;;; <decl> -> <signdecl>
  6. ;;;        -> <valdef>
  7.  
  8. ;;; decl contains value declarations and type signatures.(
  9. ;;; type related decls are topdecls and are separated from
  10. ;;; these decls.
  11.  
  12. (define-struct decl   
  13.   (include ast-node))
  14.                       
  15.  
  16.  
  17. ;;; <signdecl> -> <vars> :: [<context> =>] <type>
  18. ;;;
  19. ;;; <vars>     -> <var> , ... , <var>
  20. ;;;
  21.  
  22. (define-struct signdecl ; this affixes a signature to a list of variables
  23.   (include decl)
  24.   (predicate signdecl?)
  25.   (slots
  26.    (vars (type (list var-ref)))
  27.    (signature (type signature))))
  28.  
  29. ;;; This is introduced into decl lists by dependency analysis
  30. (define-struct recursive-decl-group
  31.   (include decl)
  32.   (slots
  33.    ;; none of these are recursive decl groups
  34.    (decls (type (list decl)))
  35.    ))
  36.  
  37. ;;; <valdef>  -> <lhs> = <exp> [where { <decls> [;] }]
  38. ;;;           -> <lhs> <gdrhs> [where { <decls> [;] }]
  39. ;;;
  40. ;;; <lhs>     -> <apat>
  41. ;;;           -> <funlhs>
  42. ;;;
  43. ;;; <funlhs>  -> <afunlhs>
  44. ;;;           -> <pat> <varop> <pat>
  45. ;;;           -> <lpat> <varop> <pat>
  46. ;;;           -> <pat> <varop> <rpat>
  47. ;;;
  48. ;;; <afunlhs> -> <var> <apat>
  49. ;;;           -> ( <funlhs> ) <apat>    (infix operator with more than 2 args)
  50. ;;;           -> <afunlhs> <apat>       (multiple argument pattern)
  51.  
  52. (define-struct valdef  ; this defines values.
  53.   (include decl)
  54.   (predicate valdef?)
  55.   (slots
  56.    ;; this pattern contains all new variables defined.
  57.    ;; For a function definition the pattern will always
  58.    ;; be a simple variable.
  59.    (lhs (type pattern))
  60.    ;; this is a list of right hand sides.
  61.    ;; for a pattern definition, this list is always a singleton.  For
  62.    ;; a function definition, there is a member for every successive
  63.    ;; alternative for the function.
  64.    (definitions (type (list single-fun-def)))
  65.    ;; this is used internally by dependency analysis
  66.    (depend-val (type int) (uninitialized? #t))
  67.    ;; this is filled in by the type phase
  68.    (dictionary-args (type (list var)) (uninitialized? #t))
  69.    ;; used for defaulting
  70.    (module (type symbol) (default '|Prelude|))
  71.    (extra-decls (type (list exp)) (default '()))
  72.    ))
  73.  
  74. (define-struct single-fun-def
  75.   (include ast-node)
  76.   (slots
  77.    ;; this list is always empty for pattern definition
  78.    ;; and always non-empty for function definition.
  79.    ;; The length of this list is the arity of the function.
  80.    ;; All single-fun-defs for a function have the same arity.
  81.    (args (type (list pattern)))
  82.    ;; <gdrhs>, this contains a list of guard , expression pairs
  83.    (rhs-list (type (list guarded-rhs)))
  84.    ;; this contains declarations local to the
  85.    ;; single fun def.  It scopes over the args.  The
  86.    ;; guarded-rhs may refer to these values.
  87.    (where-decls (type (list decl)))
  88.    ;; true when declared in infix style.  Used for printing
  89.    ;; and to check precs in prec parsing.
  90.    (infix? (type bool) (bit #t))
  91.    (avoid-printing? (type bool) (default '#f) (bit #t))
  92.    ))
  93.  
  94.  
  95.  
  96. ;;; <gdrhs>   -> <gd> = <exp> [<gdrhs>]
  97. ;;;
  98. ;;; <gd>      -> | <exp>
  99.  
  100. (define-struct guarded-rhs ; a single guarded expression.  A special expression
  101.   (include ast-node)
  102.   (slots
  103.    ;; node - omitted-guard - is used when no guard given
  104.    (guard (type exp))
  105.    (rhs (type exp))))
  106.  
  107.  
  108. ;;; Some examples of the above:
  109. ;;; (a,b) | z>y = (z,y)
  110. ;;;       | otherwise = (1,2)
  111. ;;;   where z = x-2
  112. ;;;
  113. ;;;  valdef:
  114. ;;;    lhs = (a,b)
  115. ;;;    definitions =
  116. ;;;       [single-fun-def:
  117. ;;;         args = []
  118. ;;;         rhs-list = [guarded-rhs: guard = z>y
  119. ;;;                                  rhs = (z,y),
  120. ;;;                     guarded-rhs: guard = otherwise
  121. ;;;                                  rhs = (1,2)]
  122. ;;;         where-decls = [valdef: lhs = z
  123. ;;;                                definitions =
  124. ;;;                                   [single-fun-def:
  125. ;;;                                      args = []
  126. ;;;                                      rhs-list = [guarded-rhs:
  127. ;;;                                                    guard = omitted-guard
  128. ;;;                                                    exp = x-2]
  129. ;;;                                      where-decls = []]]]
  130. ;;;
  131. ;;;  fact 0 = 1
  132. ;;;  fact (n+1) = (n+1)*fact n
  133. ;;;
  134. ;;;  valdef:
  135. ;;;    lhs = fact
  136. ;;;    definitions =
  137. ;;;       [single-fun-def:
  138. ;;;         args = [0]
  139. ;;;         rhs-list = [guarded-rhs: guard = omitted-guard
  140. ;;;                                  rhs = 1]
  141. ;;;         where-decls = [],
  142. ;;;        single-fun-def:
  143. ;;;         args = [n+1]
  144. ;;;         rhs-list = [guarded-rhs: guard = omitted-guard
  145. ;;;                                  rhs = (n+1)*fact n]
  146. ;;;         where-decls = []]
  147.  
  148.  
  149.  
  150.  
  151. ;;; Definitions for patterns
  152.  
  153. ;;; This is a simplification; the real syntax is complicated by
  154. ;;; rules for precedence and associativity.
  155. ;;;
  156. ;;; <pat>   -> <pat> <conop> <pat>           pcon
  157. ;;;         -> <pat> + <integer>             plus-pat
  158. ;;;         -> - <integer-or-float>          *** ???  const-pat?
  159. ;;;         -> <apat>
  160. ;;;         -> <con> <apat> .... <apat>      pcon
  161. ;;;
  162. ;;; <apat>  -> <var>                         var-pat
  163. ;;;         -> <var> @ <apat>                as-pat
  164. ;;;         -> <con>                         *** ??? var-pat?
  165. ;;;         -> <literal>                     const-pat
  166. ;;;         -> _                             wildcard-pat
  167. ;;;         -> ()                            pcon special case
  168. ;;;         -> ( <pat> )                     (grouping syntax)
  169. ;;;         -> ( <pat> , ... , <pat> )       pcon special case
  170. ;;;         -> [ <pat> , ... , <pat> ]       list-pat
  171. ;;;         -> ~ <apat>                      irr-pat
  172.  
  173. (define-struct pattern
  174.   (include ast-node))
  175.  
  176. (define-struct apat
  177.   (include pattern))
  178.  
  179. (define-struct as-pat  ;; var@pat
  180.   (include apat)
  181.   (slots
  182.    (var (type var-ref))
  183.    (pattern (type pattern))))
  184.  
  185. (define-struct irr-pat ;; ~pat
  186.   (include apat)
  187.   (slots
  188.    (pattern (type pattern))))
  189.  
  190. (define-struct var-pat  ;; v
  191.   (include apat)
  192.   (predicate var-pat?)
  193.   (slots
  194.    (var (type var-ref))))
  195.  
  196. (define-struct wildcard-pat  ;; _
  197.   (include apat)
  198.   (predicate wildcard-pat?))
  199.  
  200. (define-struct const-pat  ;; literal
  201.   (include apat)
  202.   (predicate const-pat?)
  203.   (slots
  204.    (value (type const))
  205.    ;; this is the code that actually performs the match.
  206.    ;; it's filled in by type phase.
  207.    (match-fn (type exp) (uninitialized? #t))))
  208.  
  209. (define-struct plus-pat  ;; p+k
  210.   (include pattern)
  211.   (slots
  212.    (pattern (type pattern))
  213.    (k (type integer))
  214.    ;; code to check for match, filled in by type phase
  215.    (match-fn (type exp) (uninitialized? #t))
  216.    ;; code to bind result, filled in by type phase
  217.    (bind-fn (type exp) (uninitialized? #t))
  218.    ))
  219.  
  220. (define-struct pcon      ;; con pat1 pat2 ...
  221.   (include pattern)      ;; pat1 con pat2
  222.   (predicate pcon?)
  223.   (slots
  224.    (name (type symbol))
  225.    (con (type def))
  226.    (pats (type (list pattern)))
  227.    (infix? (type bool) (bit #t))))
  228.  
  229. (define-struct list-pat   ;; [p1,p2,...]
  230.   (include apat)
  231.   (slots
  232.    (pats (type (list pattern)))))
  233.  
  234. (define-struct dynamic-pat
  235.   (include pattern)
  236.   (slots 
  237.    (pat (type pattern))
  238.    (sig (type signature))
  239.    (runtime-vars (type (list var)) (default '()))))
  240.  
  241. ;;; The following structs deal with prec parsing of patterns.
  242.  
  243. (define-struct pp-pat-list
  244.   (include pattern)
  245.   (slots
  246.    (pats (type (list pattern)))))
  247.  
  248. (define-struct pp-pat-plus
  249.   (include pattern)
  250.   (predicate pp-pat-plus?))
  251.  
  252. (define-struct pp-pat-negated
  253.   (include pattern)
  254.   (predicate pp-pat-negated?))
  255.  
  256.  
  257.  
  258. ;;; Structs for annotations
  259.  
  260. (define-struct annotation
  261.   (include decl)
  262.   (predicate annotation?))
  263.  
  264. (define-struct annotation-decl
  265.   (include annotation)
  266.   (predicate annotation-decl?)
  267.   (slots
  268.    (names (type (list symbol)))
  269.    (annotations (type (list annotation-value)))))
  270.  
  271. (define-struct annotation-value
  272.   (include annotation)
  273.   (predicate annotation-value?)
  274.   (slots
  275.    (name (type symbol))
  276.    (args (type (list t)))))
  277.  
  278. ;;; This is a list of annotations placed in where decls lists in the same
  279. ;;; manner a signdecls.
  280.  
  281. (define-struct annotation-decls
  282.   (include annotation)
  283.   (predicate annotation-decls?)
  284.   (slots
  285.     (annotations (type (list annotation)))))
  286.